package net.csdn.business.common.domain;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import lombok.Data;
import net.csdn.business.common.constant.CommonConstants;
import net.csdn.business.common.enums.ResultCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;

import java.io.Serializable;

/**
 * @author likun
 * @date 2022/6/6 19:32
 */
@Data
public class ResponseResult<T> implements Serializable {
    private int code;

    private String errorName;

    private String msg;

    private T data;

    private String traceId = MDC.get(CommonConstants.TRACE_ID);

    public ResponseResult() {
        super();
    }

    public ResponseResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResponseResult(ResultCodeEnum respCodeEnum, T data) {
        this.code = respCodeEnum.getCode();
        this.msg = respCodeEnum.getMsg();
        this.errorName = respCodeEnum.name();
        this.data = data;
    }

    public static <T> ResponseResult<T> build(int code, String msg) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static <T> ResponseResult<T> build(int code, String errorName, String msg) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(code);
        result.setMsg(msg);
        result.setErrorName(errorName);
        return result;
    }

    public static <T> ResponseResult<T> success(T result) {
        ResponseResult<T> res = new ResponseResult<>();
        res.setCode(ResultCodeEnum.SUCCESS.getCode());
        res.setMsg(ResultCodeEnum.SUCCESS.getMsg());
        res.setData(result);
        return res;
    }

    public static <T> ResponseResult<T> success() {
        ResponseResult<T> res = new ResponseResult<>();
        res.setCode(ResultCodeEnum.SUCCESS.getCode());
        res.setMsg(ResultCodeEnum.SUCCESS.getMsg());
        return res;
    }

    public static <T> ResponseResult<T> fail(ResultCodeEnum resultCodeEnum) {
        ResponseResult<T> res = new ResponseResult<>();
        res.setCode(resultCodeEnum.getCode());
        res.setMsg(resultCodeEnum.getMsg());
        res.setErrorName(resultCodeEnum.name());
        return res;
    }

    public static <T> ResponseResult<T> fail() {
        ResponseResult<T> res = new ResponseResult<>();
        res.setCode(ResultCodeEnum.FAIL.getCode());
        res.setMsg(ResultCodeEnum.FAIL.getMsg());
        res.setErrorName(ResultCodeEnum.FAIL.name());
        return res;
    }

    public static <T> ResponseResult<T> fail(int code, String msg) {
        ResponseResult<T> res = new ResponseResult<>();
        res.setCode(code);
        res.setMsg(msg);
        res.setErrorName(ResultCodeEnum.UN_KNOW.name());
        res.setData(null);
        return res;
    }

    public static <T> ResponseResult<T> build(ResultCodeEnum resultCode) {
        return build(resultCode.getCode(), resultCode.name(), resultCode.getMsg());
    }

    public static <T> ResponseResult<T> build(ResultCodeEnum resultCode, String msg) {
        return build(resultCode.getCode(), resultCode.name(), StringUtils.isNotBlank(msg) ? msg : resultCode.getMsg());
    }

    public static <T> ResponseResult<JSONObject> result(ResultCodeEnum respCodeEnum, T data) {
        JSONObject dataJson = new JSONObject();
        dataJson.put("code",respCodeEnum.getCode());
        dataJson.put("msg",respCodeEnum.getMsg());
        dataJson.put("data",data);

        return new ResponseResult<JSONObject>(respCodeEnum,dataJson);
    }

    public static <T> ResponseResult<JSONObject> successResult(T data) {
        JSONObject dataJson = new JSONObject();
        dataJson.put("code",ResultCodeEnum.SUCCESS.getCode());
        dataJson.put("msg",ResultCodeEnum.SUCCESS.getMsg());
        dataJson.put("data",data);

        return new ResponseResult<JSONObject>(ResultCodeEnum.SUCCESS,dataJson);
    }
}